package com.luzhikun.path.com.luzhikun.myExamSystem.dao;

/**
 * ClassName: UserDao
 * Description:
 * date: 2021/4/22 18:18
 *
 * @author luzhikun
 * @since JDK 1.8
 */

import com.luzhikun.path.com.luzhikun.myExamSystem.model.User;
import com.luzhikun.path.com.luzhikun.myExamSystem.util.DBCPUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户类Dao层
 */

public class UserDao {

//    DBCPUtil util = new DBCPUtil();

    /*
     * 用户注册
     *
     * */
    public boolean addUser(User user) {
        //创建连接对象
        Connection conn = null;
        //创建预处理对象
        PreparedStatement pst = null;
        //获取连接
        conn = DBCPUtil.getConnection();
        //编写sql语句
        String sql = "insert into t_user(username,password,sex,email) " +
                "values(?,?,?,?)";
        //预处理对象编译sql
        try {
            pst = conn.prepareStatement(sql);
            //动态替换占位符
            pst.setString(1, user.getUserName());
            pst.setString(2, user.getPassword());
            pst.setString(3, user.getSex());
            pst.setString(4, user.getEmail());
            //返回受影响行数
            int i = pst.executeUpdate();
            return i > 0;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBCPUtil.closeAll(null, pst, conn);
        }
        return false;
    }


    /*
     * 查询
     *
     * */
    public List<User> getUsers() {
        //创建user集合
        List<User> userslist = null;
        //创建连接对象
        Connection conn = null;
        //创建预处理对象
        PreparedStatement pst = null;
        //创建结果集
        ResultSet set = null;

        //获取连接
        conn = DBCPUtil.getConnection();
        //编写sql语句
        String sql = "select * from t_user";
        //预处理对象编译sql
        try {
            pst = conn.prepareStatement(sql);
            //动态替换占位符
            //获取结果集
            ResultSet set1 = pst.executeQuery();
            //解析结果集
            if (set1 != null) {
                userslist = new ArrayList<>();
                while (set1.next()) {
                    User user = new User();
                    user.setUserId(set1.getInt("userid"));
                    user.setUserName(set1.getString("username"));
                    user.setPassword(set1.getString("username"));
                    user.setSex(set1.getString("sex"));
                    user.setEmail(set1.getString("email"));
                    userslist.add(user);
                }
            }
            return userslist;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBCPUtil.closeAll(set, pst, conn);
        }
        return userslist;
    }

    /*
     * 删除操作
     *
     * */
    public boolean deleteUser(int userId) {
        //创建连接对象
        Connection conn = null;
        //创建预处理对象
        PreparedStatement pst = null;
        //获取连接
        conn = DBCPUtil.getConnection();
        //编写sql语句
        String sql = "delete from t_user where userid=?";
        //预处理对象编译sql
        try {
            pst = conn.prepareStatement(sql);
            //动态替换占位符
            pst.setInt(1, userId);
            //返回受影响行数
            int i = pst.executeUpdate();
            return i > 0;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBCPUtil.closeAll(null, pst, conn);
        }
        return false;
    }

    /**
     * 登录操作
     */
    public boolean login(String userName, String passWord) {
        //创建连接
        Connection conn = null;
        //创建预处理对象
        PreparedStatement pst = null;
        //创建结果集
        ResultSet rs = null;
        //获取连接
        conn = DBCPUtil.getConnection();
        //编写sql
        String sql = "select * from t_user where username=? and password=?";
        int count = 0;
        //编译sql
        try {
            pst = conn.prepareStatement(sql);
            //动态替换占位符
            pst.setString(1, userName);
            pst.setString(2, passWord);
            //获取结果集
            rs = pst.executeQuery();
            //判断
            if (rs != null && rs.next()) {
                return true;
            }
//            if (set!=null){
//                return true;
//            }
//            while (set.next()){
//                count = set.getInt("count(*)");
//            }
//            if (set.next()){
//                return true;
//            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }//关闭通道
        finally {
            DBCPUtil.closeAll(rs, pst, conn);
        }
        return false;
    }

}
